package com.managertrade.service.sqllite.impl;

import com.managertrade.dao.sqlite.entity.LocalLog;
import com.managertrade.dao.sqlite.repository.LocalLogRepository;
import com.managertrade.service.sqllite.service.LocalLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class LocalLogServiceImpl implements LocalLogService {
    @Autowired
    LocalLogRepository logRepository;
    @Override
    @Transactional("sqliteTransactionManager")
    public LocalLog addLog(String msg) {
        LocalLog log = new LocalLog();
        log.setMessage(msg);
        log.setCreatedAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        LocalLog save = logRepository.save(log);
        return save;
    }

    @Override
    @Transactional(value = "sqliteTransactionManager", readOnly = true)
    public List<LocalLog> getLogs() {
        return logRepository.findAll();
    }
}
